﻿using System.Collections.Generic;
using ExBlogger.Domain;
using ExBlogger.Domain.Repositories;
using NHibernate.Criterion;
using NHibernate.Transform;
using SharpArch.NHibernate;

namespace ExBlogger.Repository {
    public class CommentsRepository : NHibernateRepository<Comment>, ICommentsRepository {
        public IList<CommentListItem> GetListItems(int blogId) {
            var query = Session.CreateCriteria<Comment>()
                .CreateAlias("CommentedBy", "u")
                .CreateAlias("CommentedOn", "p")
                .Add(Restrictions.Eq("p.PostedOn.Id", blogId))
                .SetProjection(Projections.ProjectionList()
                                .Add(Projections.Property("p.Title"), "PostTitle")
                                .Add(Projections.Property("Content"), "CommentContent")
                                .Add(Projections.Property("u.Username"), "UserName")
                                .Add(Projections.Property("u.Id"), "UserId")
                                .Add(Projections.Property("CommentDate"), "CommentDate"))
                 .SetResultTransformer(Transformers.AliasToBean<CommentListItem>());

            return query.List<CommentListItem>();
        }
    }
}

